package com.auto.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannelDemo02 {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("D:\\file01.txt");
        FileChannel fisChannel = fis.getChannel();

        FileOutputStream fos = new FileOutputStream("F:\\file02.txt");
        FileChannel fosChannel = fos.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true) {
            //将文件中数据读取到缓冲区中
            int read = fisChannel.read(byteBuffer);
            if (read == -1) {
                break;
            }
            byteBuffer.flip();
            //将缓冲区中文件写入到文件中
            fosChannel.write(byteBuffer);
        }
        fis.close();
        fos.close();
    }

}
